########## Supplementary materials 4 #############

## This code replicates the graphs from the qualitative appendix (SM4)


## Packages etc

rm(list=ls())

library(fixest)
library(dplyr)
library(ggplot2)
library(sf)
library(gridExtra)

#### Load data

adm2 <- readRDS("adm2.rds") ## Shapefiles of Malawian districts (second order admin unit)


####### FIGURE 7 (map of focus group locations) ##########

adm2 <- adm2 %>%
  mutate(Sampled = ifelse(NAME_1 %in% 
                            c("Blantyre", "Thyolo", "Lilongwe",
                              "Ntchisi", "Mchinji"), "Sampled", "Not sampled"))

ggplot(adm2) +
  geom_sf(col="black", aes(fill=Sampled)) +
  scale_fill_manual(values = c("white", "darkred")) +
  theme_classic() +
  theme(axis.title.x = element_blank(), 
        axis.title.y = element_blank(), 
        axis.text.x = element_blank(),
        axis.text.y = element_blank(), 
        axis.ticks.x = element_blank(), 
        axis.ticks.y = element_blank(), 
        axis.line.x = element_blank(), 
        axis.line.y = element_blank(), 
        legend.title=element_blank())

####### FIGURE 8 (distribution of incumbent support/rejection in sampled areas) ########

ps_info <- readRDS("ps_info.rds")

fg_2019_ev <- ps_info %>%
  mutate(dpp_share = dpp_share, 
         rejected_share = rejected_share) %>%
  mutate(Sampled = ifelse(district %in% 
                            c("Blantyre 21", "Thyolo 24", "Lilongwe 13",
                              "Ntchisi 09", "Mchinji 12"), "Sampled", "Not sampled")) 

fg_2019_sampled <- fg_2019_ev %>%
  filter(Sampled == "Sampled")

fg_2019_not_sampled <- fg_2019_ev %>%
  filter(Sampled == "Not sampled")

dpp_plot <- ggplot(data=fg_2019_ev, aes(x=dpp_share, col=Sampled, fill=Sampled)) +
  geom_density(position="dodge", alpha=0.6) +
  scale_fill_manual(values = c("grey10", "darkred")) +
  scale_colour_manual(values = c("grey10", "darkred")) +
  theme_minimal() +
  geom_vline(xintercept = mean(fg_2019_not_sampled$dpp_share, na.rm=T), col="grey10") +
  geom_vline(xintercept = mean(fg_2019_sampled$dpp_share, na.rm=T), col="darkred") +
  xlab("DPP share 2019 (%)") + ylab("Density") +
  theme(axis.ticks.y = element_blank(), 
        axis.text.y = element_blank(), 
        legend.position = "bottom") +
  ggtitle("a) DPP support")


reject_plot <- ggplot(data=fg_2019_ev, 
                      aes(x=rejected_share, col=Sampled, fill=Sampled)) +
  geom_density(position="dodge", alpha=0.6) +
  scale_fill_manual(values = c("grey10", "darkred")) +
  scale_colour_manual(values = c("grey10", "darkred")) +
  theme_minimal() +
  geom_vline(xintercept = 
               mean(fg_2019_not_sampled$rejected_share, na.rm=T), col="grey10") +
  geom_vline(xintercept = 
               mean(fg_2019_sampled$rejected_share, na.rm=T), col="darkred") +
  scale_x_continuous(limits = c(0, 5)) +
  xlab("Rejection rate 2019 (%)") + ylab("Density") +
  theme(axis.ticks.y = element_blank(), 
        axis.text.y = element_blank(), 
        legend.position = "bottom") +
  ggtitle("b) Rejection rate")

grid.arrange(dpp_plot, reject_plot, nrow=1)




